package com.example.batch.writer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.batch.item.ItemWriter;

import com.example.batch.api.DemoDTO;

public class DBWriter implements ItemWriter<DemoDTO> {
	@Override
	public void write(List<? extends DemoDTO> items) throws Exception {
		Connection connection = DriverManager.getConnection("jdbc:mysql:///test", "root", "root");
		PreparedStatement prepareStatement = connection.prepareStatement("insert demo_table values(?,?)");
		items.forEach(dto -> {
			try {
				prepareStatement.setInt(1, dto.getI());
				prepareStatement.setString(2, dto.getStr());
				prepareStatement.addBatch();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		});
		prepareStatement.executeBatch();
	}


}
